#导入
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from .models import User

class UsernameMobileAuthBackend(ModelBackend):

    # 重写该方法，实现多账号验证
    def authenticate(self, request, username=None, password=None, **kwargs):
        """
        功能：根据身份信息验证用户身份
        参数：request请求对象；username前端传入的用户名或手机号等；password密码；
        返回值：返回用户对象或None
        """
        try:
            # 1、根据username、mobile去查找用户
            # select * from tb_users where username='18588269037' or mobile='18588269037';
            user = User.objects.get(
                Q(username=username) | Q(mobile=username) # username='18588269037' or mobile='18588269037'
            )
            # user = User.objects.get(username=username, mobile=username) # username=18588269037 and mobile=18588269037
        except User.DoesNotExist as e:
            return None


        #判断此次登陆请求是否是管理站点登陆，如果是需要校验is_staff=True
        if request is None:
            if not user.is_staff:
                return None  #表示不允许登陆

        # 2、检查密码
        if user.check_password(password):
            return user

from django.conf import settings
from meiduo_mall.utils.secret import SecretOauth
#获取验证邮件完成的verify_url
def generate_verify_email_url(request):
    """
    :param request: 请求对象－request.user
    :return: 完成的verify_url
    """
    #1、获取当前登陆的用户
    user = request.user
    #2、把用户数据加密成token值
    data_dict = {
        'user_id': user.id,
        'email': user.email
    }
    auth = SecretOauth()
    token = auth.dumps(data_dict)
    verify_url = settings.EMAIL_VERIFY_URL + token
    #3、返回完成的验证url
    return verify_url

def jwt_response_payload_handler(token, user=None, request=None):
    """
    Returns the response data for both the login and refresh views.
    Override to return a custom response such as including the
    serialized representation of the User.

    Example:

    def jwt_response_payload_handler(token, user=None, request=None):
        return {
            'token': token,
            'user': UserSerializer(user, context={'request': request}).data
        }

    """
    return {
        'username': user.username,
        'user_id': user.id,
        'token': token
    }


